import axios from 'axios';

const RequestUrl: any = {
  'dev': 'https://dev.cn',
  'sit': 'https://sit.cn',
  'prod': 'https://prod.cn'
}

const NODE_ENV: string = `${import.meta.env.VITE_APP_NODE_ENV}`

const instance = axios.create({
  baseURL: RequestUrl[NODE_ENV],
  timeout: 3000,
});

/**
 * http request 拦截器
 */
 instance.interceptors.request.use(
  (config: any) => {
    config.data = JSON.stringify(config.data);
    config.headers = {
      "Content-Type": "application/json",
    };
    return config;
  },
  (error) => {
    return Promise.reject(error);
  }
);

/**
 * https response 拦截器
 */
 instance.interceptors.response.use(
  (response) => {
    if (response.data.errCode === 2) {
      console.log("过期");
    }
    return response;
  },
  (error) => {
    console.log("请求出错：", error);
  }
);

const Request = (config: any) => {
  return new Promise(() => {
    instance({
      ...config,
    })
  })
}

export default Request;

